﻿using System;
using System.Xml;
using XmlStudio.DataLayer;
using XmlStudio.Interfaces;
using XmlStudio.Models.Events;
using XmlStudio.Utilities;

namespace XmlStudio.Models.Actions {
    /// <summary>
    /// Action to remove attribute from a BigXmlNode.
    /// </summary>
    internal sealed class RemoveAttributeAction : XmlModelAction {
        #region Fields
        #region Private
        /// <summary>
        /// Node where to remove attribute.
        /// </summary>
        private BigXmlNodeInternal node;

        /// <summary>
        /// Name of the prefix.
        /// </summary>
        private string prefix;

        /// <summary>
        /// Name of the attribute to remove.
        /// </summary>
        private string name;

        /// <summary>
        /// Namespace of the removed attribute.
        /// </summary>
        private string namespaceURI;

        /// <summary>
        /// Value of the attribute to remove.
        /// </summary>
        private string value;

        /// <summary>
        /// Indicates whether attribute has a namespace.
        /// </summary>
        private bool withNamespace = false;
        #endregion
        #endregion

        #region Constructors
        #region Public
        /// <summary>
        /// Initializes a new instance of the <see cref="RemoveAttributeAction"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="node">Reference to the <see cref="BigXmlNode"/> which to remove an attribute from.</param>
        /// <param name="name">Name of an attribute to remove.</param>
        public RemoveAttributeAction(IXmlModelWithNotifications model, BigXmlNodeInternal node, string name)
            : this(model, node, string.Empty, name, string.Empty) {
            this.withNamespace = false;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RemoveAttributeAction"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="node">Reference to the <see cref="BigXmlNode"/> which to remove an attribute from.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="localName">Name of the attribute to remove.</param>
        /// <param name="namespaceURI">Namespace of the attribute to remove.</param>
        public RemoveAttributeAction(IXmlModelWithNotifications model, BigXmlNodeInternal node, string prefix, string localName, string namespaceURI)
            : base(model) {
            this.node = node;
            this.wasModified = node.Modified;
            this.prefix = prefix;
            this.name = localName;
            this.namespaceURI = string.IsNullOrEmpty(namespaceURI) ? string.Empty : namespaceURI;
            this.withNamespace = true;
            this.address = node.Address.DeepClone<int[]>();
        }
        #endregion
        #endregion

        #region Methods
        #region Protected
        /// <summary>
        /// Executes the action.
        /// </summary>
        protected override void ExecuteCore() {
            if(withNamespace) {
                this.value = this.node.XmlNode.Attributes[this.name, this.namespaceURI].Value;

                var element = this.node.XmlNode as XmlElement;
                if(element != null) {
                    IBigXmlAttribute attribute;
                    string attributeName = BigXmlAttribute.CreateKey(this.prefix, this.name, this.namespaceURI);
                    this.node.Attributes.TryGetValue(attributeName, out attribute);
                    if(attribute == null) { throw new InvalidOperationException("Attribute to remove not found."); }

                    this.node.RemoveAttribute(this.name, this.namespaceURI, this.prefix);
                    element.RemoveAttribute(this.name, this.namespaceURI);
                    this.node.SetModified(true);

                    this.model.SetModified(true);
                    this.model.OnAttributeRemoved(new AttributeRemovedEventArgs<BigXmlNode, IBigXmlAttribute>(this.node, attribute));
                    this.model.OnRawXmlChanged(new RawXmlChangedEventArgs());
                }
            } else {
                this.value = ((XmlNode)this.node.XmlNode).Attributes[this.name].Value;

                var element = this.node.XmlNode as XmlElement;
                if(element != null) {
                    IBigXmlAttribute attribute;
                    //string attributeName = string.Format("{0}:{1}", this.name, this.namespaceURI);
                    string attributeName = BigXmlAttribute.CreateKey(this.prefix, this.name, this.namespaceURI);
                    this.node.Attributes.TryGetValue(attributeName, out attribute);
                    if(attribute == null) { throw new InvalidOperationException("Attribute to remove not found."); }

                    this.node.RemoveAttribute(this.name, this.namespaceURI, this.prefix);
                    element.RemoveAttribute(this.name);
                    this.node.SetModified(true);

                    this.model.SetModified(true);
                    this.model.OnAttributeRemoved(new AttributeRemovedEventArgs<BigXmlNode, IBigXmlAttribute>(this.node, attribute));
                    this.model.OnRawXmlChanged(new RawXmlChangedEventArgs());
                }
            }
        }

        /// <summary>
        /// Executes the undo for the current action.
        /// </summary>
        protected override void UnExecuteCore() {
            if(withNamespace) {
                var element = this.node.XmlNode as XmlElement;
                if(element != null) {
                    var attribute = new BigXmlAttribute(this.prefix, this.name, this.namespaceURI, this.value);
                    this.node.AddAttribute(attribute);
                    element.SetAttribute(this.name, this.namespaceURI, this.value);
                    this.node.SetModified(wasModified);

                    this.model.SetModified(this.modelWasModified);
                    this.model.OnAttributeAdded(new AttributeAddedEventArgs<BigXmlNode, IBigXmlAttribute>(this.node, attribute));
                    this.model.OnRawXmlChanged(new RawXmlChangedEventArgs());
                }
            } else {
                var element = this.node.XmlNode as XmlElement;
                if(element != null) {
                    var attribute = new BigXmlAttribute(this.name, this.value);
                    this.node.AddAttribute(attribute);
                    element.SetAttribute(this.name, this.value);
                    this.node.SetModified(wasModified);

                    this.model.SetModified(this.modelWasModified);
                    this.model.OnAttributeAdded(new AttributeAddedEventArgs<BigXmlNode, IBigXmlAttribute>(this.node, attribute));
                    this.model.OnRawXmlChanged(new RawXmlChangedEventArgs());
                }
            }
        }
        #endregion
        #endregion
    }
}